﻿Public Class FrmGiaThue

    Private Sub FrmGiaThue_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SetCbBox(cbbienso, "BienSo", "BienSo", Xe.DanhSachBienSo)
        SetCbBox(cbhinhthuc, "TenHT", "MaHT", DonGiaThue.DanhSachHT)
        dgvDonGia.DataSource = DonGiaThue.DanhSach
    End Sub

    Private Sub btnNhapLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhapLai.Click
        txtDonGia.Text=""
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        If txtDonGia.Text <> "" Then
            If Not DonGiaThue.ThemDG(cbbienso.SelectedValue, cbhinhthuc.SelectedValue, txtDonGia.Text) Then
                MsgBox("Không thể thêm dữ liệu!", MsgBoxStyle.Critical, "Lỗi")
            End If
            dgvDonGia.DataSource = DonGiaThue.DanhSach
        Else
            MsgBox("Hãy điền thông tin đầy đủ !", MsgBoxStyle.Information, "Thông báo")
        End If

    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        If btnSua.Text = "&Sửa" Then
            cbbienso.SelectedValue = dgvDonGia.CurrentRow.Cells("BienSo").Value
            cbhinhthuc.SelectedValue = dgvDonGia.CurrentRow.Cells("TenHT").Value
            txtDonGia.Text = dgvDonGia.CurrentRow.Cells("DonGia").Value
            btnSua.Text = "&Lưu"
        Else
            btnSua.Text = "&Sửa"
            If Not DonGiaThue.capnhat(cbbienso.SelectedValue, cbhinhthuc.SelectedValue, txtDonGia.Text) Then
                MsgBox("Bạn không thể lưu dữ liệu!", MsgBoxStyle.Critical, "Lỗi")
            End If
            dgvDonGia.DataSource = DonGiaThue.DanhSach
            btnNhapLai_Click(sender, e)
        End If
    End Sub

    Private Sub dgvDonGia_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDonGia.CellDoubleClick
        btnSua.Text = "&Sửa"
        btnSua_Click(sender, e)
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Dispose()
    End Sub

    Private Sub btnTimKiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimKiem.Click
        dgvDonGia.DataSource = DonGiaThue.timkiem(txtTuKhoa.Text)
    End Sub

    Private Sub btndaydu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndaydu.Click
        btnSua.Text = "&Sửa"
        dgvDonGia.DataSource = DonGiaThue.DanhSach()
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        btnSua.Text = "&Sửa"
        If Not DonGiaThue.xoa(cbbienso.SelectedValue) Then
            MsgBox("Bạn không thể xóa dữ liệu này !", MsgBoxStyle.Critical, "Lỗi")
        End If
        dgvDonGia.DataSource = DonGiaThue.DanhSach()
    End Sub
End Class